package app

import (
	"fmt"
	"go-chromedp-client/service"
	"net/http"
	"net/http/pprof"
	"os"
	"time"
)

func HttpServer() {
	fs := http.FileServer(http.Dir(os.Getenv("ROOT_PATH") + string(os.PathSeparator) + "web"))
	http_port := os.Getenv("http_port")
	if http_port == "" {
		http_port = "8091"
	}
	fmt.Println("HttpServer http_port", http_port)
	http.Handle("/", http.StripPrefix("/", fs))
	http.HandleFunc("/index.html", func(w http.ResponseWriter, r *http.Request) {
		f, err := os.Open(os.Getenv("ROOT_PATH") + string(os.PathSeparator) + "web" + string(os.PathSeparator) + "index.html")
		if err != nil {
			// handle error
			fmt.Println(err)
			return
		}
		http.ServeContent(w, r, "index.html", time.Now(), f)
	})
	http.HandleFunc("/debug/pprof/index", pprof.Index)
	http.HandleFunc("/api/bookDownload", service.DownLoad)
	http.HandleFunc("/api/bookList", service.BookList)
	http.HandleFunc("/api/bookMenuDownload", service.MenuDownLoad)

	http.HandleFunc("/api/comicMenu", service.ComicMenuApi)
	http.HandleFunc("/api/comicPage", service.ComicUrlApi)
	http.HandleFunc("/api/comicList", service.ComicList)
	http.HandleFunc("/api/comicOne", service.ComicOne)
	err := http.ListenAndServe(":"+http_port, nil)

	if err != nil {
		fmt.Print(err.Error())
	}
}
